program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils, CustApp
  { you can add units after this }
  , DbEntities
  , mssql
  ;

type

  { TDdlDump }

  TDdlDump = class(TCustomApplication)
  protected
    procedure DoRun; override;
    procedure ParseDb;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure WriteHelp; virtual;
  end;

{ TDdlDump }

procedure TDdlDump.DoRun;
var
  ErrorMsg: String;
begin
  // quick check parameters
  ErrorMsg:=CheckOptions('h','help');
  if ErrorMsg<>'' then begin
    ShowException(Exception.Create(ErrorMsg));
    Terminate;
    Exit;
  end;

  // parse parameters
  if HasOption('h','help') then begin
    WriteHelp;
    Terminate;
    Exit;
  end;

  { add your program here }
  ParseDb;

  // stop program loop
  Terminate;
end;

procedure TDdlDump.ParseDb;
var
  i: Integer;
  vDb: TDbDatabase;
  vDbInfo: TDbHostInfo;
begin
  vDbInfo.DbName:='SisTO';
  vDbInfo.Host:='SAWO';
  vDbInfo.UsrName:='sa';
  vDbInfo.UsrPass:='masterkey';

  with TSqlParser.Create do
  begin
    Parse([], vDbInfo, vDb);
    for i := 0 to vDb.Views.Count-1 do
      WriteLn(vDb.Views[i].Name + ': ' + vDb.Views[1].DDL);
    vDb.Free;
  end;
end;

constructor TDdlDump.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  StopOnException:=True;
end;

destructor TDdlDump.Destroy;
begin
  inherited Destroy;
end;

procedure TDdlDump.WriteHelp;
begin
  { add your help code here }
  writeln('Usage: ',ExeName,' -h');
end;

var
  Application: TDdlDump;
begin
  Application:=TDdlDump.Create(nil);
  Application.Title:='DDL Dump';
  Application.Run;
  Application.Free;
end.

